/* colour-panel.vala
 *
 * Copyright (C) 2010  Richard Talbot-Watkins
 * Copyright (C) 2010  Dave Jeffery
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * Authors:
 *  Richard Talbot-Watkins <richtw1@gmail.com>
 * 	Dave Jeffery <david.richard.jeffery@gmail.com>
 */

using Gtk;
using Cairo;

/**
 * The ColourPanel widget draws a 33 x 33 pixel square filled with the 
 * Cario RGB value specified in its rgb property. Whenever the rgb
 * property is changed, the widget is updated. The widget emits a 
 * mouse button press event when clicked. 
 *
 * @author Dave Jeffery <david.richard.jeffery@gmail.com>
 * @version 2010.1209
 * @since 0.1
 */
private class MonsterDesigner.ColourPanel : DrawingArea {
	/**
	 * The logical colour displayed by the widget. 
	 */
	private int logical_colour;

	/**
	 * The palette data used to display the widget. 
	 */
	private PaletteData palette_data;

	/**
	 * Constructor for the ColourPanel widget.
	 *
	 * @param rgb a CarioColour value for the widget to display
	 */     
	public ColourPanel (int logical_colour, PaletteData palette_data) {
		this.logical_colour = logical_colour;
		this.palette_data = palette_data;
		set_size_request (33, 33);
		add_events (Gdk.EventMask.BUTTON_PRESS_MASK);

		/* Redraw the widget whenever the palette changed. */
		palette_data.changed.connect (redraw_canvas);
	}

	/**
	 * Expose event handler. 
	 *
	 * The expose event is emitted when the ColourPanel widget is asked to
	 * (re)paint itself.
	 * 
	 * @return false to pass signal to the default handler
	 */
	public override bool expose_event (Gdk.EventExpose event) {
		var cr = Gdk.cairo_create (this.window);
		cr.rectangle (event.area.x, event.area.y,
						event.area.width, event.area.height);
		cr.clip ();
		draw (cr);
		return false;
	}

	/**
	 * This method is called when the colour panel wants to repaint 
	 * itself.
	 */
	private void redraw_canvas () {
		if (this.window == null) {
			return;
		}

		unowned Gdk.Region region = this.window.get_clip_region ();
		this.window.invalidate_region (region, true);
		this.window.process_updates (true);
	}

	/**
	 * Draws the colour panel using the Cairo vector graphics library.
	 */
    private void draw(Context cr) {
		var rgb = palette_data.logical_to_cairo (logical_colour);

		cr.save ();
		cr.rectangle (0, 0, this.allocation.width, this.allocation.height);
		cr.set_source_rgb (rgb.red, rgb.green, rgb.blue);
		cr.fill_preserve ();
		cr.restore ();
	}
}
